package pl.edu.agh.portal.application.portlets;

import java.io.IOException;
import java.util.ArrayList;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletConfig;
import javax.portlet.PortletException;
import javax.portlet.PortletMode;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import pl.edu.agh.portal.application.utils.PortletsUtils;
import pl.edu.agh.thesis.webservices.employee.EmployeeManagerSkeleton;
import pl.edu.agh.thesis.webservices.employee.client.EmployeeManagerStub;
import pl.edu.agh.thesis.webservices.employee.client.EmployeeUtils;
import pl.edu.agh.thesis.webservices.project.client.ProjectManagerStub;
import pl.edu.agh.thesis.webservices.project.client.ProjectUtils;
import pl.edu.agh.thesis.webservices.task.TaskManagerSkeleton;
import pl.edu.agh.thesis.webservices.task.client.TaskManagerStub;
import pl.edu.agh.thesis.webservices.task.client.TaskUtils;

public class ProjectDetailsPortlet extends ThesisGenericPortlet {
	
	private static final String NORMAL_VIEW = "/project_details_portlet.jsp";
	
	private PortletRequestDispatcher normalView;
			
	public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {
		
		int employeeId = EmployeeUtils.validate(PortletsUtils.temp_session);
		ProjectManagerStub.Project project = ProjectUtils.get(PortletsUtils.temp_session, Integer.parseInt((String)PortletsUtils.vault.get("projectId")) );
				
		String restriction = employeeId + " " + project.getId();
		TaskManagerStub.Task[] tasksAssignee = TaskUtils.getAll(PortletsUtils.temp_session, restriction, TaskManagerSkeleton.PROJECT_ID_RESTRICTION); 
		TaskManagerStub.Task[] tasksReporter = getAllProjectTaskForReporter(employeeId, project.getId());
		request.setAttribute(PortletsUtils.ATTRIBUTE_PROJECT_FOR_DETAILS, project);
		request.setAttribute(PortletsUtils.ATTRIBUTE_PROJECT_DETAILS_TASKS_ASSIGNEE, tasksAssignee);
		request.setAttribute(PortletsUtils.ATTRIBUTE_PROJECT_DETAILS_TASKS_REPORTER, tasksReporter);
						
		normalView.include(request, response);
	}
	
	@Override
	public void processAction(ActionRequest request, ActionResponse response) throws PortletException, IOException {
		
		String edit = request.getParameter(PortletsUtils.BUTTON_EDIT);
		String cancel = request.getParameter(PortletsUtils.BUTTON_CANCEL);		
		String details = request.getParameter(PortletsUtils.BUTTON_DETAILS);
		String remove = request.getParameter(PortletsUtils.BUTTON_REMOVE);
		String createTask = request.getParameter(PortletsUtils.BUTTON_CREATE_TASK);
		
		if (cancel != null) {			
			response.sendRedirect("/portal/portal/user_main.psml");
		} else if (edit != null) {
			response.sendRedirect("/portal/portal/project_edit.psml");
		} else if (details != null) {
			PortletsUtils.vault.put("taskId", details);
			response.sendRedirect("/portal/portal/task_details.psml");
		} else if (remove != null) {
			PortletsUtils.vault.put("taskId", details);
			response.sendRedirect("/portal/portal/task_remove.psml");
		} else if (createTask != null) {
			response.sendRedirect("/portal/portal/task_create.psml");
		} else {
			response.setPortletMode(PortletMode.VIEW);
		}
	}	
	
	public void init(PortletConfig config) throws PortletException {
		super.init(config);
		normalView = config.getPortletContext().getRequestDispatcher(NORMAL_VIEW);
	}
	
	public void destroy() {
		normalView = null;
		super.destroy();
	}
	
	private TaskManagerStub.Task[] getAllProjectTaskForReporter(int employeeId, int projectId) {
		ArrayList<TaskManagerStub.Task> list = new ArrayList<TaskManagerStub.Task>();
		
		EmployeeManagerStub.Employee[] employeesInProject = EmployeeUtils.getAll(PortletsUtils.temp_session, EmployeeManagerSkeleton.GET_EMPLOYEE_IN_PROJECT, String.valueOf(projectId));
		
		if (employeesInProject != null) { 
			for (EmployeeManagerStub.Employee employee : employeesInProject) {				
				TaskManagerStub.Task[] projectTasks = TaskUtils.getAll(PortletsUtils.temp_session, employee.getId() + " " + String.valueOf(projectId), TaskManagerSkeleton.PROJECT_ID_RESTRICTION); 
				if (projectTasks != null) {
					for (TaskManagerStub.Task task : projectTasks) {
						if (task.getReporter() == employeeId) {
							list.add(task);
						}
					}		
				}
			}
		}
		if (list.size() == 0) return null;			
		return list.toArray(new TaskManagerStub.Task[0]);
	}
	
}
